﻿using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.TestManagement.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;


namespace Test_Case_Marker
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private TfsTeamProjectCollection _tfs = null;
        private WorkItemStore _store = null;
        ITestManagementTeamProject _testproject = null;
        public static ITestManagementService test_service = null;
        public static ITestPlan TP = null;
                
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this._tfs = null;
            test_suite_label.Content = null;
            tfs_text.Content = null;
            Test_Plan_Sel.Items.Clear();
            treeView_suite.Items.Clear();
            Load_TC.IsEnabled = false;
            
            TeamProjectPicker tpp = new TeamProjectPicker(TeamProjectPickerMode.SingleProject, false);
            if (tpp.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

                if (tpp.SelectedTeamProjectCollection != null)
                {
                    this._tfs = tpp.SelectedTeamProjectCollection;
                    test_service = (ITestManagementService)_tfs.GetService(typeof(ITestManagementService));
                    _store = (WorkItemStore)_tfs.GetService(typeof(WorkItemStore));
                    if (tpp.SelectedProjects[0] == null)
                    {

                        MessageBox.Show("Select a valid Team Project");
                        return;
                    }
                    string proj_name = tpp.SelectedProjects[0].Name;
                    _testproject = test_service.GetTeamProject(proj_name);


                    if (_testproject != null)
                    {

                        tfs_text.Content = "connected to " + this._tfs.Name + '\\' + _testproject.ToString();
                        Test_Plan_Sel.IsEnabled = true;
                        Get_TestPlans(_testproject);
                        if (Test_Plan_Sel.Items.Count == 0)
                        {
                            MessageBox.Show("No Test Plans Found");
                            
                        }

                    }
                    else
                        MessageBox.Show("Please select a valid Team Project");
                }
                
            }
            else
                MessageBox.Show("This action has been cancelled by the user");
           
        }

        void Get_TestPlans(ITestManagementTeamProject project)
        {
            
            foreach(ITestPlan tp in project.TestPlans.Query("Select * from TestPlan"))
              Test_Plan_Sel.Items.Add(tp.Name + " <ID: " + tp.Id.ToString() + " >");            
            
        }

 

        private void Test_Plan_Sel_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
           
            if (Test_Plan_Sel.SelectedValue != null)
            {
                test_suite_label.Content = "Please select one of the below identified test suites";
                treeView_suite.Items.Clear();
                Load_TC.IsEnabled = false;
                GetTestSuites_Plan(get_ID(Test_Plan_Sel.SelectedValue.ToString()));
            }
        }

        int get_ID(string name)
        {

            int ID = Convert.ToInt32(name.Substring((name.LastIndexOf("<ID: ") + 5), (name.LastIndexOf(" >") - (name.LastIndexOf("<ID: ") + 5))));

            return ID;
        }

        void GetTestSuites_Plan(int ID)
        {

        TP = _testproject.TestPlans.Find(ID);
            if (TP != null)
            {
                if (TP.RootSuite != null)
                {

                    TreeViewItem root = new TreeViewItem();
                    root.Header = TP.RootSuite.Title.ToString() + " <ID: " + TP.RootSuite.Id.ToString() + " >";
                    treeView_suite.Items.Add(root);

                    GetSuiteEntries(TP.RootSuite.SubSuites, root);


                }
               
               
            }
            else
                MessageBox.Show("unable to retrieve Test Plan information");            

        }


        void GetSuiteEntries(ITestSuiteCollection suite_entries, TreeViewItem root)
        {
            foreach (ITestSuiteBase suite in suite_entries)
            {

                if (suite != null)
                {
                    TreeViewItem suite_tree = new TreeViewItem();
                    suite_tree.Header = suite.Title.ToString() + " <ID: " + suite.Id.ToString() + " >";
                    root.Items.Add(suite_tree);


                    if (suite.TestSuiteType == TestSuiteType.StaticTestSuite)
                    {
                        IStaticTestSuite suite1 = suite as IStaticTestSuite;
                        if (suite1 != null && (suite1.SubSuites.Count > 0))
                        {

                            GetSuiteEntries(suite1.SubSuites, suite_tree);
                        }
                    }
                }

            }
        }

      
        private void Load_TC_Click(object sender, RoutedEventArgs e)
        {


            if (treeView_suite.SelectedItem != null)
            {
                ITestSuiteBase Test_Suite = _testproject.TestSuites.Find(get_ID(treeView_suite.SelectedItem.ToString()));
                ITestCaseCollection TestCases = Test_Suite.AllTestCases;
                if (TestCases.Count == 0)
                    MessageBox.Show("No Test Cases Found");
                else
                {

                    Test_Case[] TestCase_List = new Test_Case[TestCases.Count];
                    int i = 0;
                    foreach (ITestCase Testcase in TestCases)
                    {
                        TestCase_List[i++] = new Test_Case(Testcase.Id, Testcase.Title);                        
                    }

                    Show_Test_Cases TestCase_Grid = new Show_Test_Cases();
                    TestCase_Grid.TC_DataGrid.ItemsSource = TestCase_List;
                    this.Hide();
                    TestCase_Grid.Cast_Obj(this);
                    TestCase_Grid.Show();
                }
            }
            else
                MessageBox.Show("Please select a test suite");
           
        }

        private void treeView_suite_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
          Load_TC.IsEnabled = true;
        }

       
        
    }
}
